package com.itheima.reggie.controller;

import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.ShoppingCart;
import com.itheima.reggie.service.ShoppingCartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Map;

@RestController
@RequestMapping("/shoppingCart")
@ResponseBody
public class ShoppingCartController {
    @Autowired
    ShoppingCartService shoppingCartService;

    @GetMapping("/list")
    public R shoppingCartListByUserId(HttpSession session){
        Long userId = (Long) session.getAttribute("user");
        return R.success(shoppingCartService.selectListById(userId));
    }
    @PostMapping("/add")
    public R addShoppingCart(@RequestBody ShoppingCart shoppingCart,HttpSession session){
        Long userId = (Long) session.getAttribute("user");
        shoppingCart.setUserId(userId);
        shoppingCartService.saveCart(shoppingCart);
        return R.success("保存成功");
    }
    @PostMapping("/sub")
    public R subShoppingCart(@RequestBody Map<String,Long> map){
        Long setmealId = map.get("setmealId");
        Long dishId = map.get("dishId");
        shoppingCartService.subCart(setmealId,dishId);
        return R.success("删除成功");
    }
    @DeleteMapping("/clean")
    public R cleanCart(HttpSession session){
        Long userId = (Long) session.getAttribute("user");
        shoppingCartService.cleanAllCart(userId);
        return R.success("清空购物车成功");
    }
}
